﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Pong_Project.Game_objects
{
    abstract class Powerup : GameObject
    {
        private int timeAlive, timeDead, timeLasted, timeToLast, timeToLive, minTimeBeforeSpawning, maxTimeBeforeSpawning;
        private GameObject objectThatCanPickItUp;

        public Powerup(GameObjectIdentity identity, GameObject objectThatCanPickItUp, Vector2 position, int timeToLast, int timeToLive, int minTimeBeforeSpawning, int maxTimeBeforeSpawning)
            : base(identity, position)
        {
            this.objectThatCanPickItUp = objectThatCanPickItUp;
            this.timeToLast = timeToLast;
            this.timeToLive = timeToLive;
            this.minTimeBeforeSpawning = minTimeBeforeSpawning;
            this.maxTimeBeforeSpawning = maxTimeBeforeSpawning;
            timeAlive = 0;
            timeDead = 1;
            LayerDepth = 0.98f;
        }

        public int TimeAlive
        {
            get { return timeAlive; }
        }

        public int TimeDead
        {
            get { return timeDead; }
        }

        public int TimeLasted
        {
            get { return timeLasted; }
        }

        public int TimeToLast
        {
            set { timeToLast = value; }
            get { return timeToLast; }
        }

        public int TimeToLive
        {
            set { timeToLive = value; }
            get { return timeToLive; }
        }

        public int MinimumTimeBeforeSpawning
        {
            set { minTimeBeforeSpawning = value; }
            get { return minTimeBeforeSpawning; }
        }

        public int MaximumTimeBeforeSpawning
        {
            set { maxTimeBeforeSpawning = value; }
            get { return maxTimeBeforeSpawning; }
        }

        private void SetAlive(bool alive)
        {
            timeAlive = (alive ? 1 : 0);
            timeDead = (alive ? 0 : 1);
        }

        public override void Update(GameTime gameTime)
        {
            if (timeDead == 1)
            {
                PowerupManager.Remove(this);
            }
            if (timeLasted > 0) // Checks if the powerup has been picked up, and its effect is lasting
            {
                if (timeLasted > timeToLast) // Checks if the powerups effect has lasted long enough to be removed
                {
                    RemoveEffect();
                }
                else
                {
                    timeLasted++;
                }

            }
            if (timeAlive > 0) // Checks if the powerup is alive, placed on the board
            {
                if (timeAlive > timeToLive) // Checks if the powerup has been alive for too long, without being picked up
                {
                    timeAlive = 0;
                    timeDead = 1;
                }
                else
                {
                    timeAlive++;
                }
            }
            else
            {
                if (timeDead > maxTimeBeforeSpawning) // Checks if the powerup has waited too long before it can be spawned
                {
                    Spawn();
                    timeAlive = 1;
                    timeDead = 0;
                }
                else if (timeDead > minTimeBeforeSpawning) // Checks if the powerup has waited long enough before it can be spawned
                {
                    if (Game.Random.Next(0, 10000) < 50) // Decides if the powerup can spawn now
                    {
                        Spawn();
                        timeAlive = 1;
                        timeDead = 0;
                    }
                }
                else if (timeDead > 0) // Checks if the powerup is dead: not placed on the board, and not in effect
                {
                    timeDead++;
                }
            }
            base.Update(gameTime);
        }

        public override void Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch)
        {
            if (timeAlive > 0) // Checks if the powerup is alive, placed on the board
            {
                base.Draw(spriteBatch);
            }
        }

        public override void Reset()
        {
            timeAlive = 0;
            timeDead = 1;
            timeLasted = 0;
            ScaleX = 1.0f;
            ScaleY = 1.0f;
            Rotation = 0.0f;
            LayerDepth = 0.98f;
        }

        public virtual void Spawn()
        {
            PowerupManager.Add(this);
        }

        public virtual void ApplyEffect(GameObject gameObject)
        {
            timeAlive = 0;
            timeDead = 1;
            if (timeToLast > 0) // Checks if the powerup has an effect that last only for some time
            {
                timeLasted = 1;
            }
        }

        public virtual void RemoveEffect()
        {
            timeLasted = 0;
        }
    }
}
